package cn.itsource.config;

import cn.itsource.web.interceptor.LoginCheckInterceptor;
import cn.itsource.web.interceptor.PermissionCheckInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**--------------------------------------------------------
针对web的配置
 @Configuration:Spring的配置标签
 --------------------------------------------------------**/
@Configuration
public class WebConfig implements WebMvcConfigurer{

    @Autowired
    private LoginCheckInterceptor loginCheckInterceptor ;
    @Autowired
    private PermissionCheckInterceptor permissionCheckInterceptor ;

    /**--------------------------------------------------------
    登录页面的视图跳转
     --------------------------------------------------------**/
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        /** 访问 /login ，跳转 /login.html **/
        registry.addViewController("/login.html").setViewName("/login.html");
        registry.addViewController("/nopermission.html").setViewName("/views/nopermission.html");
    }

    /**--------------------------------------------------------
    注册拦截器
     --------------------------------------------------------**/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginCheckInterceptor)
                //拦截所有资源
                .addPathPatterns("/**")
                //放行资源
                .excludePathPatterns(
                        "/login",
                        "/login.html",
                        "/assets/**"
                );

        registry.addInterceptor(permissionCheckInterceptor)
                //拦截所有资源
                .addPathPatterns("/**")
                //放行资源
                .excludePathPatterns(
                        "/login",
                        "/login.html",
                        "/assets/**"
                );
    }
}
